package xyz.cerberu.cet.subscription.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.cerberu.cet.global.domain.ExceptionEncode;
import xyz.cerberu.cet.global.domain.Result;
import xyz.cerberu.cet.global.utils.ResultUtils;
import xyz.cerberu.cet.subscription.domain.Subscription;
import xyz.cerberu.cet.subscription.service.SubtitleSubscriptionServerImpl;
import xyz.cerberu.cet.user.domain.User;

import java.util.List;

/**
 * @author β世界
 * Created on 15:59
 * @Description 订阅的控制器
 */
@Controller
@RequestMapping("/subscription")
public class SubscriptionController {

    private final SubtitleSubscriptionServerImpl subtitleSubscriptionServerImpl;

    @Autowired
    public SubscriptionController(SubtitleSubscriptionServerImpl subtitleSubscriptionServerImpl) {
        this.subtitleSubscriptionServerImpl = subtitleSubscriptionServerImpl;
    }

    /**
     * @author β世界 on 10:19 2021/3/15
     * 添加一个订阅
     * @param sub: 字幕组的id
     * @return xyz.cerberu.cet.global.domain.Result<java.lang.Object>
     **/
    @RequiresUser
    @ResponseBody
    @RequestMapping(value = "/add" ,method = RequestMethod.POST)
    public Result<Object> addSubscription(int sub){
        String uid = ((User) SecurityUtils.getSubject().getPrincipal()).getUid();
        if(subtitleSubscriptionServerImpl.isSubscription(uid,sub)){
            return ResultUtils.genErrorResult(ExceptionEncode.SUBSCRIPTION_STATUS_EXCEPTION,"你已订阅，请不要重复订阅");
        }

        // 添加用户的订阅
        subtitleSubscriptionServerImpl.add(uid,sub);
        return ResultUtils.genSuccessResult("添加成功");
    }

    @RequiresUser
    @ResponseBody
    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    public Result<Object> deleteSubscription(int sub){
        String uid = ((User) SecurityUtils.getSubject().getPrincipal()).getUid();
        if(!subtitleSubscriptionServerImpl.isSubscription(uid,sub)){
            return ResultUtils.genErrorResult(ExceptionEncode.SUBSCRIPTION_STATUS_EXCEPTION,"没有订阅，无法删除");
        }
        // 删除信息
        subtitleSubscriptionServerImpl.remove(uid,sub);
        return ResultUtils.genSuccessResult("删除成功");
    }

    /**
     * @author β世界 on 7:42 2020/7/7
     * 检查一个用户是否订阅了当前的字幕组
     * @param sub: 字幕组的ID
     * @return boolean
     **/
    @RequiresUser
    @ResponseBody
    @RequestMapping(value = "/isSubscription",method = RequestMethod.GET)
    public Result<Object> isSubscription(int sub){
        String uid = ((User) SecurityUtils.getSubject().getPrincipal()).getUid();
        System.out.println(subtitleSubscriptionServerImpl);
        boolean result = subtitleSubscriptionServerImpl.isSubscription(uid,sub);
        // 请求状态
        return ResultUtils.genSuccessResult(result);
    }

    /**
     * @author β世界 on 16:56 2020/6/25
     * 获取一个用户订阅的所有信息
     * @return com.Cet.common.domain.Result
     **/
    @ResponseBody
    @RequestMapping(value = "/subinfo",method = RequestMethod.GET)
    @RequiresUser
    public Result<Object> getSubscriptionInfo(){
        String uid = ((User) SecurityUtils.getSubject().getPrincipal()).getUid();
        // 获取到这个用户订阅的所有信息
        List<Subscription> subscriptions = subtitleSubscriptionServerImpl.getSubscriptionInfo(uid);
        if(subscriptions.size() <= 0 ){
            return ResultUtils.genErrorResult(ExceptionEncode.SUBSCRIPTION_STATUS_EXCEPTION,"你并没有订阅内容，请先添加订阅");
        }
        return ResultUtils.genSuccessResult(subscriptions);
    }

}
